﻿$.extend({

    UMUtilities: {

        SetListBoxHeight: function (objectId) {

            $(objectId).attr("size", function () { return this.options.length; });
        },

        StringTrim: function (sval) {

            if (sval == null) {
                sval = '';
            }

            sval = sval.replace('null', '');

            return sval.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
        },

        SortByName: function (a, b) {

            var aName = a.GroupName; //.toLowerCase();
            var bName = b.GroupName; //.toLowerCase();

            return ((aName < bName) ? -1 : ((aName > bName) ? 1 : 0));
        },


        ValidateText: function (text) {

            // validate group name for special characters
            var characterReg = /^[A-Za-z\s\d=&-]+$/;
            if (!characterReg.test(text)) {
                return false;
            }

            return true;
        },

        SortUnique: function (arryUsers, fieldName) {

            arryUsers.sort($.UMUtilities.DynamicSort(fieldName));

            var ret = [arryUsers[0]];
            for (var i = 1; i < arryUsers.length; i++) {
                if (arryUsers[i - 1].UserName !== arryUsers[i].UserName) {
                    ret.push(arryUsers[i]);
                }
            }

            return ret;
        },

        DynamicSort: function (property) {

            var sortOrder = 1;
            if (property[0] === "-") {
                sortOrder = -1;
                property = property.substr(1);
            }

            return function (a, b) {
                var result = (a[property] < b[property]) ? -1 : (a[property] > b[property]) ? 1 : 0;
            }
        },

        EncodeText: function (textVal) {

            return textVal.replace('&', '&amp;');
        },

        DecodeText: function (textVal) {

            return textVal.replace('&amp;', '&');
        },
    }
});


jQuery.fn.filterByText = function (textbox, selectSingleMatch) {
    return this.each(function () {
        var select = this;
        var options = [];
        $(select).find('option').each(function () {
            options.push({ value: $(this).val(), text: $(this).text() });
            //alert($(this).val() + "-" + $(this).text());
        });
        $(select).data('options', options);
        $(textbox).bind('change keyup', function () {
            var options = $(select).empty().data('options');
            var search = $(this).val().trim();
            var regex = new RegExp(search, "gi");

            $.each(options, function (i) {
                var option = options[i];
                if (option.text.match(regex) !== null) {
                    $(select).append(
                           $('<option>').val(option.value).html($.UMUtilities.EncodeText(option.text))
                        );
                }
            });
            if (selectSingleMatch === true && $(select).children().length === 1) {
                $(select).children().get(0).selected = true;
            }
        });
    });
};